package com.example.douyinproject.video;
/*
 * @Description:
 * @Author: 山志凯
 * @Date: 2021-06-30
 * @Time: 14:14
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.douyinproject.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.jzvd.JzvdStd;
import de.hdodenhof.circleimageview.CircleImageView;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VH> {
    Context context;
    List<VideoBean.ItemsBean> list;

    public VideoAdapter(Context context, List<VideoBean.ItemsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_video,parent,false);
        VH vh = new VH(view);
        return vh;
    }

    //将控件进行展示
    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        VideoBean.ItemsBean itemsBean = list.get(position);
        holder.likeTv.setText(itemsBean.getVotes().getUp()+"");
        holder.msgTv.setText(itemsBean.getComments_count()+"");
        holder.shareTv.setText(itemsBean.getShare_count()+"");
        holder.contentTv.setText(itemsBean.getContent());
        holder.nameTv.setText(itemsBean.getUser().getLogin());
        String userPic = itemsBean.getUser().getMedium();//用户头像
        Picasso.with(context).load(userPic).error(R.mipmap.ic_launcher).into(holder.iv);
        String high_url = itemsBean.getHigh_url();  //视频播放的地址
        String pic_url = itemsBean.getPic_url();   //视频没有播放时的缩略图
        holder.jzvdStd.setUp(high_url,"",JzvdStd.SCREEN_FULLSCREEN);
        ImageView thumbIv = holder.jzvdStd.posterImageView;
        Picasso.with(context).load(pic_url).into(thumbIv); //加载缩略图
        holder.jzvdStd.positionInList = position;
        if (position == 0) {
            holder.jzvdStd.startVideo();
        }
    }

    @Override
    public int getItemCount() { return list.size(); }
    class VH extends RecyclerView.ViewHolder{
        JzvdStd jzvdStd;   //媒体播放器
        TextView likeTv,shareTv,msgTv,contentTv,nameTv;
        CircleImageView iv;
        public VH(@NonNull View itemView) {
            super(itemView);
            jzvdStd = itemView.findViewById(R.id.item_video_jzvd);
            likeTv = itemView.findViewById(R.id.item_video_tv_like);
            shareTv = itemView.findViewById(R.id.item_video_tv_share);
            msgTv = itemView.findViewById(R.id.item_video_tv_msg);
            contentTv = itemView.findViewById(R.id.item_video_tv_content);
            nameTv = itemView.findViewById(R.id.item_video_tv_name);
            iv = itemView.findViewById(R.id.item_video_iv);
        }
    }
}

